//
//  CCYMeNewTableViewCell.m
//  模拟-百思不得姐
//
//  Created by 程春玉 on 16/8/9.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "CCYMeNewTableViewCell.h"
#import <AVFoundation/AVFoundation.h>

@interface CCYMeNewTableViewCell()

@property (nonatomic,strong) AVPlayer *player;
@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, assign) BOOL isPlay;

@end

@implementation CCYMeNewTableViewCell


// 普通图片时的方法
- (void)setMessageWithModel:(CCYMeSubscribModel *)model
{
    [self changeMessageWithModel:model];
    if ([model.type isEqualToString:@"gif"]) {
        [self.img sd_setImageWithURL:[NSURL URLWithString:[model.gif[@"images"] firstObject]]];
        UIImageView *tagImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        tagImg.image = [UIImage imageNamed:@"common-gif"];
        [self.MyContView addSubview:tagImg];
    } else {
        [self.img sd_setImageWithURL:[NSURL URLWithString:[model.image[@"big"] firstObject]]];
    }
}


- (void)changeMessageWithModel:(CCYMeSubscribModel *)model
{
    [self.imgV sd_setImageWithURL:[NSURL URLWithString:[model.u[@"header"] lastObject]]];
    self.name.text = model.u[@"name"];
    self.time.text = model.passtime;
    self.title.text = model.text;
    self.tagLabel.text = @"";
    for (NSDictionary *temp in model.tags) {
        NSString *str = temp[@"name"];
        self.tagLabel.text = [[self.tagLabel.text stringByAppendingString:@" "] stringByAppendingString:str];
    }
    self.img = [[UIImageView alloc] initWithFrame:self.MyContView.bounds];
    [self.MyContView addSubview:self.img];
}

// 带视频时候的方法
- (void)setVideoMessageWithModel:(CCYMeSubscribModel *)model
{
    [self changeMessageWithModel:model];
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:[model.video[@"download"] firstObject]]];
    self.player = [[AVPlayer alloc]init];
    [self.player replaceCurrentItemWithPlayerItem:item];
    
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = self.MyContView.bounds;
    playerLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//视频填充模式
    [_MyContView.layer addSublayer:playerLayer];
    [_MyContView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvtion:)]];
    
    [self.img sd_setImageWithURL:[NSURL URLWithString:[model.video[@"thumbnail"] firstObject]]];
}

- (void)tapAvtion:(UITapGestureRecognizer *)tap
{
    [self.img removeFromSuperview];
    if (_isPlay) {
        [self.player pause];
    } else {
        [self.player play];
    }
    _isPlay = !_isPlay;
}

    
- (void)setDataWithModel:(JXPRecommendModel *)model{
    NSURL *url = [NSURL URLWithString:model.profile_image];
    UIImage *ima = [UIImage imageNamed:@"AppIcon40x40"];
    [self.imgV sd_setImageWithURL:url placeholderImage:ima];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.imgV.layer.cornerRadius = 20.f;
    self.imgV.clipsToBounds = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.zanBtn addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.hateBtn addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.shareBtn addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.commendBtn addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
}


- (void)action:(UIButton *)btn
{
    XMGLog(@"点击了按钮");
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
